﻿

namespace Common.Algorithm.Core.LocalTest;

public class SimpleGeneticFunctionExecute
{
    public static int[] Run()
    {
        SimpleGeneticFunctionService service = new SimpleGeneticFunctionService(TargetFunc);
        Action action = () =>
        {
            service.RunAlgm(500);
        };
        Console.WriteLine($"time:{TimeService.TimeWatch(action)}");

        var best = service.bestSequence;
        Console.WriteLine($"{string.Join("", best)}");
        Console.WriteLine($"{string.Join("|", targetFunc2D.BinaryDecode(best))}=>{TargetFunc(best)}");

        return best;
    }

    public static void RunMany(int num)
    {
        for ( int i = 0; i < num; i++ )
        {
            Run();
        }
    }

    static ITargetFunction targetFunc2D = new HimmelblauFunction( );
    public static double TargetFunc(int[] factors)
    {
        var func = targetFunc2D.GenMathFunction();
        var doubles = targetFunc2D.BinaryDecode(factors);
        return func(doubles);
    }

}
